// packageB/settlement/index.js
import moment from 'moment';
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        filter(type, options) {
            if (type === 'hour') {
                return options.filter((option) => option > 7 && option < 22);
            }
            return options;
        },
        formatter(type, val) {
            if (type === 'year') {
                return val + '年';
            }
            if (type === 'month') {
                return val + '月';
            }
            if (type === 'day') {
                return val + '日';
            }
            if (type === 'hour') {
                return val + '时';
            }
            return val;
        },
        minHour: '',
        maxHour: '',
        show: false,
        serviseDetail: '',
        resourceURL: wx.api.host + '/',
        oid: '',
        send_id: '',
        form: {
            make_time: '',
            service_id: '',
            address_id: "",
            quantity: 1
        },
        allPrice:1,
        defaultaddress: "",
        currentDate: ''
    },
    //选择数量
    onChange(e) {
        var num =(e.detail*this.data.serviseDetail.sale_price).toFixed(2)


        console.log(e.detail,num);
        this.setData({
            'form.quantity': Number(e.detail),
            allPrice:num
        })
    },
    //获取接下来七天时间
    getTime() {

        let day1 = moment().add(1, 'days').format('YYYY-MM-DD');
        day1 = moment(`${day1} 08:00:00`).valueOf();
        let day2 = moment().add(7, 'days').format('YYYY-MM-DD');
        day2 = moment(`${day2} 22:00:00`).valueOf();

        this.setData({
            maxDate: day2,
            minDate: day1,
            'form.make_time': moment(day1).format('YYYY-MM-DD HH:mm:ss'),
        })


    },
    //确认选择时间
    confirmTime(event) {

        // if (moment(event.detail).format('HH:mm:ss')=='00:00:00') {
        //     console.log( moment(event.detail).format('HH:mm:ss')); 
        // }

        this.setData({
            'form.make_time': moment(event.detail).format('YYYY-MM-DD HH:mm:ss'),
            show: false
        })

    },
    //获取默认收货地址
    getdefaultaddress() {
        wx.http.get(wx.api.getdefaultaddress, {}).then(res => {

            if (res.code == 1) {

                this.setData({
                    defaultaddress: res.data,
                    'form.address_id': res.data.address_id
                })
            } else {
                // Toast.fail(res.msg);
            }
        })
    },
    //选择地址界面
    chooseAddress() {
        wx.navigateTo({
            url: '/pages/address/index?type=1',
        })
    },
    onClose() {
        this.setData({
            show: false
        })
    },
    onOpen() {
        this.setData({
            show: true
        })
    },
    // 下单
    onPayment() {
        wx.http.post(wx.api.downorder, {
            ...this.data.form,
            pay_end: 2
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    send_id: res.data.send_id
                })
                this.requestPayment(res.data)
            } else {
                Toast.fail(res.msg)
            }
        })
    },

    //支付
    requestPayment(data) {
        wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            success: (res) => {

                Toast.success({
                    message: '支付成功',
                    duration: 1000,
                    forbidClick: true
                })
                setTimeout(() => {

                    wx.redirectTo({
                        url: '/packageB/paymentSuccess/index?send_id=' + this.data.send_id,
                    })
                }, 1000);
            },
            fail: (res) => {
                Toast.fail({
                    message: '支付失败',
                    duration: 1000,
                    forbidClick: true
                })
                // setTimeout(() => {
                //     wx.redirectTo({
                //         url: '/packageB/serviceOrderList/index',
                //     })
                // }, 1000);
            },

        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let serviseDetail = wx.getStorageSync('serviseDetail')
        this.setData({
            serviseDetail,
            'form.service_id': serviseDetail.service_id,
            allPrice:serviseDetail.sale_price
        })

        this.getTime()

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow(options) {

        if (this.data.defaultaddress) {
            this.setData({
                'form.address_id': this.data.defaultaddress.address_id
            })

        } else {
            this.getdefaultaddress()
        }




    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        // wx.removeStorageSync('serviseDetail')
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})